#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    vector<int> arr;

public:
    bool findTarget(TreeNode* root, int k)
    {
        InOrder(root);
        int left = 0, right = arr.size() - 1;
        while (left < right)
        {
            if (arr[left] + arr[right] > k) right--;
            else if (arr[left] + arr[right] < k) left++;
            else return true;
        }
        return false;

    }
    void InOrder(TreeNode*& root)
    {
        if (!root) return;
        InOrder(root->left);
        arr.push_back(root->val);
        InOrder(root->right);
    }
};